/*
 * @描述: 区域管理数据请求
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
 */
import { querySysRegion, deleteSysRegion, addSysRegion, updateSysRegion, exportSysRegion, querySysRegionTree, queryRegionType } from '@/services/api';

export default {
  namespace: 'sysRegion',

  state: {
    data: {
      list: [],
      pagination: {}
    },
    treeData: {
      list: []
    },
    selectData: {
      list: []
    }
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(querySysRegion, payload);
      yield put({
        type: 'save',
        payload: response
      });
    },
    *fetchTree({ payload }, { call, put }) {
      const response = yield call(querySysRegionTree, payload);
      yield put({
        type: 'saveTree',
        payload: response
      });
    },
    *fetchRegionType({ payload }, { call, put }) {
      const response = yield call(queryRegionType, payload);
      yield put({
        type: 'saveRegionType',
        payload: response
      });
    },
    *add({ payload }, { call, put }) {
      const response = yield call(addSysRegion, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *delete({ payload }, { call, put }) {
      const response = yield call(deleteSysRegion, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *update({ payload }, { call, put }) {
      const response = yield call(updateSysRegion, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *export({ payload }, { call, put }) {
      const response = yield call(exportSysRegion, payload);
      yield put({
        type: 'save',
        payload: response
      });
      return response;
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data
      };
    },
    saveTree(state, action) {
      return {
        ...state,
        treeData: action.payload.data
      };
    },
    saveRegionType(state, action) {
      return {
        ...state,
        selectData: action.payload.data
      };
    }
  }
};
